package com.finance.service.impl;

import com.finance.domain.User;
import com.finance.service.UserService;
import com.finance.utils.StringUtil;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("userService")
public class UserServiceImpl extends BaseServiceImpl implements UserService {

    @Override
    public User selectUserByNameAndPassword(User u) {
        String pwd = u.getPassword();
        u.setPassword(StringUtil.passwordToMd5(pwd));
        return userMapper.selectUserByNameAndPassword(u);
    }

    @Override
    public int insert(User u) {
        return userMapper.insert(u);
    }

    @Override
    public User findUserByUserName(String userName) {
        return userMapper.findUserByUserName(userName);
    }


    @Override
    public int updatePassword(User u) {
        return userMapper.updatePassword(u);
    }

    @Override
    public List<User> selectAllUser(String user_id) {
        return userMapper.selectAllUser(user_id);
    }

    @Override
    public int deleteByPrimaryKey(int uid) {
        int a = userMapper.deleteByPrimaryKey(uid);
        if (a == 1) {
            moneyMapper.deleteByAuthorId(uid);
            incomeMapper.deleteByAuthorId(uid);
            return 1;
        } else {
            return 0;
        }
    }

    @Override
    public int updateByPrimaryKey(User u) {
        return userMapper.updateByPrimaryKey(u);
    }
}
